package com.wl.shop.web;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.wl.shop.entity.Dict;
import com.wl.shop.entity.FieldArticle;
import com.wl.shop.entity.FieldCategory;
import com.wl.shop.model.DataTableResult;
import com.wl.shop.model.RestResponse;
import com.wl.shop.utils.ArticleUtils;
import com.wl.shop.utils.Constants;
import com.wl.shop.utils.StringUtils;
import com.wl.shop.utils.UserUtils;

@Controller
@RequestMapping("${adminPath}/fieldCategory")
public class FieldCategoryController extends BaseController {
	
	@RequestMapping(value = "list",method = RequestMethod.GET)
	public String list(String id,Model model){
		model.addAttribute("categoryId", id);
		return "cms/field/list";
	}
	
	@RequestMapping(value = "list",method = RequestMethod.POST)
	@ResponseBody
	public DataTableResult list(String aodata,String categoryId){
		Map<String,Object>	map =Constants.convertToMap(aodata);
		int echo = (Integer) map.get("sEcho");
		DataTableResult result = new DataTableResult();
		result.setSEcho(echo);
		FieldCategory fc = new FieldCategory();
		fc.setCategoryId(categoryId);
		List<FieldCategory> listAll = fieldCategoryService.selectList(new EntityWrapper<FieldCategory>(fc).orderBy("sort"));
		result.setAaData(listAll);
		result.setITotalRecords(listAll.size());
		result.setITotalDisplayRecords(listAll.size());
		return result;
	}
	
	@RequestMapping(value = "add",method = RequestMethod.GET)
	public String add(String categoryId,Model model){
		model.addAttribute("categoryId", categoryId);
		return "cms/field/add";
	}
	
	@RequestMapping(value = "add",method = RequestMethod.POST)
	@ResponseBody
	public RestResponse add(@RequestBody FieldCategory fieldCategory){
		if(StringUtils.isBlank(fieldCategory.getCategoryId())){
			return RestResponse.failure("栏目ID不能为空");
		}
		if(StringUtils.isBlank(fieldCategory.getEnName())){
			return RestResponse.failure("英文名称不能为空");
		}
		if(StringUtils.isBlank(fieldCategory.getShowName())){
			return RestResponse.failure("显示名称不能为空");
		}
		if(StringUtils.isBlank(fieldCategory.getFieldId())){
			return RestResponse.failure("选择字段类型不能为空");
		}
		if(fieldCategoryService.isSameName(fieldCategory.getEnName(), fieldCategory.getCategoryId())){
			return RestResponse.failure("["+fieldCategory.getEnName()+"]已被使用");
		}
		Dict dict = dictService.selectById(fieldCategory.getFieldId());
		fieldCategory.setFieldName(dict.getLabel());
		fieldCategory.setLabelValue(dict.getValue());
		fieldCategory.setSort(fieldCategoryService.selectMaxSort(fieldCategory.getCategoryId())+10);
		fieldCategoryService.insert(fieldCategory);
		UserUtils.removeCache(ArticleUtils.CACHE_FIELD_MAP);
		return RestResponse.success();
	}
	
	@RequestMapping(value = "edit",method = RequestMethod.GET)
	public String edit(String id,Model model){
		FieldCategory fieldCategory = fieldCategoryService.selectById(id);
		model.addAttribute("fieldCategory", fieldCategory);
		return "cms/field/edit";
	}
	
	@RequestMapping(value = "edit",method = RequestMethod.POST)
	@ResponseBody
	public RestResponse edit(@RequestBody FieldCategory fieldCategory){
		if(StringUtils.isBlank(fieldCategory.getId())){
			return RestResponse.failure("ID不能为空");
		}
		if(StringUtils.isBlank(fieldCategory.getCategoryId())){
			return RestResponse.failure("栏目ID不能为空");
		}
		if(StringUtils.isBlank(fieldCategory.getEnName())){
			return RestResponse.failure("英文名称不能为空");
		}
		if(StringUtils.isBlank(fieldCategory.getShowName())){
			return RestResponse.failure("显示名称不能为空");
		}
		if(StringUtils.isBlank(fieldCategory.getFieldId())){
			return RestResponse.failure("选择字段类型不能为空");
		}
		FieldCategory old = fieldCategoryService.selectById(fieldCategory.getId());
		if(!old.getEnName().equals(fieldCategory.getEnName())){
			if(fieldCategoryService.isSameName(fieldCategory.getEnName(), fieldCategory.getCategoryId())){
				return RestResponse.failure("["+fieldCategory.getEnName()+"]已被使用");
			}
		}
		if(!old.getEnName().equals(fieldCategory.getEnName())){
			Map<String, Object> map = Maps.newHashMap();
			map.put("categoryId", fieldCategory.getCategoryId());
			map.put("enName", old.getEnName());
			List<FieldArticle> list = fieldArticleService.getFieldArticleByMap(map);
			if(list !=null && list.size() > 0 ){
				return RestResponse.failure("该字段英文字段已被使用,不能为更新~");
			}
		}
		fieldCategoryService.updateById(fieldCategory);
		UserUtils.removeCache(ArticleUtils.CACHE_FIELD_MAP);
		return RestResponse.success();
	}
	
	@RequestMapping(value = "delete",method = RequestMethod.POST)
	@ResponseBody
	public RestResponse delete(@RequestParam(value = "ids[]",required = false)List<String> ids){
		if(ids == null || ids.size() == 0){
			return RestResponse.failure("ID不能为空");
		}
		List<FieldCategory> fieldCategoryList = fieldCategoryService.selectBatchIds(ids);
		Map<String, Object> map = Maps.newHashMap();
		map.put("categoryId", fieldCategoryList.get(0).getCategoryId());
		map.put("fieldCategoryList", fieldCategoryList);
		List<FieldArticle> list = fieldArticleService.getFieldArticleByMap(map);
		if(list !=null && list.size() > 0 ){
			return RestResponse.failure("该字段英文字段已被使用,不能为删除~");
		}
		fieldCategoryService.deleteBatchIds(ids);
		return RestResponse.success();
	}
}
